package JDBC;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-07-20
 * Time: 15:34
 */
public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection=dataSource.getConnection();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入要修改同学的学号");
        int id=scanner.nextInt();
        System.out.println("请输入修改后的姓名");
        String name=scanner.next();
        String sql="update student set name=? where id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setInt(2,id);
        int n=preparedStatement.executeUpdate();
        preparedStatement.close();;
        connection.close();
    }
}
